package lc04;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @Author Stringzhua
 * @Date 2024/4/18 19:58
 * description:
 */
public class test02 {
    @Test
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        //创建新数组
        int[] array = new int[nums1.length + nums2.length];
        //无序数组合并, 复制元素方式实现_双指针
        for (int i = 0, k = nums1.length; i < nums1.length || k < array.length; i++, k++) {
            if (i < nums1.length) {
                array[i] = nums1[i];
            }
            if (k < array.length) {
                array[k] = nums2[k - nums1.length];
            }
        }
        //冒泡排序
        //冒泡排序
        for (int j = 0, n = array.length; j < n - 1; j++) {
            for (int k = 0; k < n - 1 - j; k++) {
                if (array[k] > array[k + 1]) {
                    //交换
                    int temp = array[k];
                    array[k] = array[k + 1];
                    array[k + 1] = temp;
                }
            }
        }
        int mid = array.length / 2;
        if (array.length % 2 == 0) {
            return (double) ((array[mid - 1] + array[mid]) / 2.0);
        } else {
            return (double) array[mid];
        }
    }

    public static void main(String[] args) {
        int[] nums1 = {1, 3};
        int[] nums2 = {2};
        test01 test01 = new test01();
        double[] array = test01.findMedianSortedArrays(nums1, nums2);
        System.out.println(Arrays.toString(array));
    }
}
